package com.pinyougou.sellergoods.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.pinyougou.mapper.TbSpecificationMapper;
import com.pinyougou.mapper.TbSpecificationOptionMapper;
import com.pinyougou.pojo.TbSpecification;
import com.pinyougou.pojo.TbSpecificationOption;
import com.pinyougou.pojo.TbSpecificationOptionExample;
import com.pinyougou.pojo.TbSpecificationOptionExample.Criteria;
import com.pinyougou.sellergoods.service.SpecificationService;
import com.pinyougou.untis.Specification;

@Service
@Transactional
public class SpecificationServiceImpl implements SpecificationService{
	@Autowired
	private TbSpecificationMapper specification;
	@Autowired
	private TbSpecificationOptionMapper specificationOption;
	//查询所有

	@Override
	public List<TbSpecification> findAll() {
		// TODO Auto-generated method stub
		return specification.selectByExample(null);
	}
	/**
	 * 添加规格和选项规格id
	 */
	@Override
	public void add(Specification specifications) {
	
		specification.insert(specifications.getSpecification());
		List<TbSpecificationOption>	 sss=specifications.getSpecificationOptionList();
		for(TbSpecificationOption s : sss) {
			s.setSpecId(specifications.getSpecification().getId());//设置规格id
			specificationOption.insert(s);
		}
	}
	
	/**
	 * 根据id查询练级查询
	 * @return
	 */
	@Override
	public Specification findOne(Long id) {
		
	TbSpecification tb=specification.selectByPrimaryKey(id);
	
	TbSpecificationOptionExample example =new TbSpecificationOptionExample();
	Criteria criteria = example.createCriteria();
	criteria.andSpecIdEqualTo(id);
	List<TbSpecificationOption> tb2 = specificationOption.selectByExample(example);
	Specification specification2 = new Specification();
	specification2.setSpecification(tb);
	specification2.setSpecificationOptionList(tb2);
		return specification2;
	}
	@Override
	public void update(Specification sp) {
//		specification.updateByPrimaryKeySelective(sp.getSpecification());
		
		specification.updateByPrimaryKey(sp.getSpecification());
		TbSpecificationOptionExample example=new TbSpecificationOptionExample();
		com.pinyougou.pojo.TbSpecificationOptionExample.Criteria criteria = example.createCriteria();
		criteria.andSpecIdEqualTo(sp.getSpecification().getId());//指定规格ID为条件
		specificationOption.deleteByExample(example);//删除	
		
		for (TbSpecificationOption ss : sp.getSpecificationOptionList()) {
			ss.setSpecId(sp.getSpecification().getId());
			specificationOption.insert(ss);	
		}
		
	}


}
